package classcourse;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import entity.classCourse;
import entity.classCourse1;
import util.HibernateUtil;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;

import classcourse.classCourseDao;
import util.HibernateUtil;
public class classCourseDaoImpl  implements classCourseDao{


@Override
public boolean addclassscourse(classCourse1 classcourse) {
	String mess = "error";
	Session session = HibernateUtil.openSession();
	try {
		Transaction transaction = session.beginTransaction();
		session.save(classcourse);
		transaction.commit();
		return true;
	} catch (Exception e) {
		//message("RegisterInfo error:" + e);
		e.printStackTrace();
		return false;
	} finally {
		if (null != session) {
			session.close();
		}
	}
}

@Override
public boolean delclasscourse(String classcoursename) {
	Session session = HibernateUtil.openSession();
	try {
		Transaction transaction = session.beginTransaction();
		Query query = session.createQuery("from classCourse where classcoursename=?");
		query.setParameter(0, classcoursename);
		classCourse classcourse = (classCourse)query.uniqueResult();
		session.delete(classcourse);
		transaction.commit();
		return true;
	} catch (Exception e){
		e.printStackTrace();
		return false;
	} finally {
		if (null != session) {
			session.close();
		}
	}
}

@Override
public boolean updateclassCourse(String id, String classcoursename, String date) {
	Session session = HibernateUtil.openSession();
	try {
		Transaction transaction = session.beginTransaction();
	    String hql ="update classCourse  set id=?,classcoursename=?,date=? where id=?";
	    Query query = session.createQuery(hql);        
	    query.setParameter(0, id);
	    query.setParameter(1, classcoursename);
	    query.setParameter(2, date);
	    query.executeUpdate();
	    session.getTransaction().commit();  
	    return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	} finally {
		if (null != session) {
			session.close();
		}
	}

}

@Override
public List<classCourse1> findAll() {
	Session session = HibernateUtil.openSession();
    Transaction transaction = null;
    try {
    	transaction = session.beginTransaction();

        
        List<classCourse1> list = session.createQuery("FROM classCourse").list(); 

        transaction.commit();
        return list;
    } catch (RuntimeException e) {
   	transaction.rollback();
        throw e;
    } finally {
		if (null != session) {
			session.close();
		}
	}
}
}
